const app = getApp()
var util = require('../../utils/util.js')
var address = require('../../utils/city.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
       menuType: 0,
       begin: null,
       status: 1,
       end: null,
       isVisible: false,
       animationData: {},
       animationAddressMenu: {},
       addressMenuIsShow: false,
       value: [0, 0, 0],
       provinces: [],
       citys: [],
       areas: [],
       province: '',
       city: '',
       area: ''
  },
addressNameInput: function (e) {
     this.setData({
          addressName: e.detail.value
     })
},
//收货人手机号
addressMobileInput: function (e) {
     this.setData({
          addressMobile: e.detail.value
     })
},
//详细地址
addressDescInput: function (e) {
     this.setData({
          addressDesc: e.detail.value
     })
},

     //提交地址
     confirmAddress: function () {
          let that = this
          let memberId = app.globalData.userInfo.id
          let receiveName = this.data.addressName
          if (util.isBlank(receiveName)) {
               if (!util.isBlank(this.data.defaultAddress)) {
                    receiveName = this.data.defaultAddress.receiveName
               }
               if (util.isBlank(receiveName)) {
                    wx.showToast({
                         title: '请输入姓名',
                         icon: 'none',
                         duration: 1500
                    })
                    return
               }
          }

          let receiveMobile = this.data.addressMobile
          if (util.isBlank(receiveMobile)) {
               if (!util.isBlank(this.data.defaultAddress)) {
                    receiveMobile = this.data.defaultAddress.receiveMobile
               }
               if (util.isBlank(receiveMobile)) {
                    wx.showToast({
                         title: '请输入电话',
                         icon: 'none',
                         duration: 1500
                    })
                    return
               }
          }
          if (!util.validatemobile(receiveMobile)) {
               return
          }
          let receiveAddress = this.data.addressDesc
          if (util.isBlank(receiveAddress)) {
               if (!util.isBlank(this.data.defaultAddress)) {
                    receiveAddress = this.data.defaultAddress.receiveAddress
               }
               if (util.isBlank(receiveAddress)) {
                    wx.showToast({
                         title: '请输入详细地址',
                         icon: 'none',
                         duration: 1500
                    })
                    return
               }
          }


          let provinceName = that.data.provinceName
          let provinceId = that.data.provinceId
          if (util.isBlank(provinceName)) {
               if (!util.isBlank(this.data.defaultAddress)) {
                    provinceName = this.data.defaultAddress.provinceName
               }
               if (util.isBlank(provinceName)) {
                    wx.showToast({
                         title: '请选择省份',
                         icon: 'none',
                         duration: 1500
                    })
                    return
               }
          }
          let cityName = that.data.cityName
          let cityId = that.data.cityId
          if (util.isBlank(cityName)) {
               if (!util.isBlank(this.data.defaultAddress)) {
                    cityName = this.data.defaultAddress.cityName
               }
               if (util.isBlank(cityName)) {
                    wx.showToast({
                         title: '请选择城市',
                         icon: 'none',
                         duration: 1500
                    })
                    return
               }
          }
          let districtName = that.data.districtName
          let districtId = that.data.districtId
          if (util.isBlank(districtName)) {
               if (!util.isBlank(this.data.defaultAddress)) {
                    districtName = this.data.defaultAddress.districtName
               }
               if (util.isBlank(cityName)) {
                    wx.showToast({
                         title: '请选择区县',
                         icon: 'none',
                         duration: 1500
                    })
                    return
               }
          }
          let isDefault = 1
          let id = 0
          if (!util.isBlank(this.data.defaultAddress)) {
               id = this.data.defaultAddress.id
               isDefault = this.data.defaultAddress.isDefault
          }
          var data = { 'id': id, 'memberId': app.globalData.userInfo.id, 'receiveName': receiveName, 'receiveMobile': receiveMobile, 'receiveAddress': receiveAddress, 'provinceName': provinceName, 'cityName': cityName, 'districtName': districtName, 'provinceId': provinceId, 'cityId': cityId, 'districtId': districtId, 'isDefault': isDefault}
          var promise = app.synPostData('address/save', data)
          promise.then(function (res) {
               if (res.data.code != 0) {
                    wx.showToast({
                         title: '系统错误',
                         icon: 'none',
                         duration: 1500
                    })
               } else {
                    wx.redirectTo({
                         url: '../address/address?cartIds=' + that.data.cartIds + '&cartType=' + that.data.cartType
                    })
               }
          }); 
         


     },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       this.setData({
            cartIds: options.cartIds,
            cartType: options.cartType
       })   
    if (options.id){
         this.loadAddress(options.id)
    }



       // 初始化动画变量
       var animation = wx.createAnimation({
            duration: 500,
            transformOrigin: "50% 50%",
            timingFunction: 'ease',
       })
       this.animation = animation;
       // 默认联动显示北京
       var id = address.provinces[0].id
       this.setData({
            provinces: address.provinces,
            citys: address.citys[id],
            areas: address.areas[address.citys[id][0].id],
       })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  loadAddress:function(id){
       let that=this
       var data = { 'addressId': id }
       var promise = app.synPostData('address/findOne', data)
       promise.then(function (res) {
            var defaultAddress=res.data.result
            var areaInfo = defaultAddress.provinceName + ',' + defaultAddress.cityName + ',' + defaultAddress.districtName
            var areaInfoId = defaultAddress.provinceId + ',' + defaultAddress.cityId + ',' + defaultAddress.districtId
            that.setData({
                 defaultAddress: res.data.result,
                 addressDesc: res.data.result.receiveAddress,
                 areaInfo: areaInfo,
                 areaInfoId: areaInfoId
            })
       }); 
  },
     // 点击所在地区弹出选择框
     selectDistrict: function (e) {
          var that = this
          if (that.data.addressMenuIsShow) {
               return
          }
          that.startAddressAnimation(true)
     },
     // 执行动画
     startAddressAnimation: function (isShow) {
          console.log(isShow)
          var that = this
          if (isShow) {
               that.animation.translateY(0 + 'vh').step()
          } else {
               that.animation.translateY(40 + 'vh').step()
          }
          that.setData({
               animationAddressMenu: that.animation.export(),
               addressMenuIsShow: isShow,
          })
     },
     // 点击地区选择取消按钮
     cityCancel: function (e) {
          this.startAddressAnimation(false)
     },
     // 点击地区选择确定按钮
     citySure: function (e) {
          var that = this
          var city = that.data.city
          var value = that.data.value
          that.startAddressAnimation(false)
          // 将选择的城市信息显示到输入框
          var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
          var areaInfoId = that.data.provinces[value[0]].id + ',' + that.data.citys[value[1]].id + ',' + that.data.areas[value[2]].id
          that.setData({
               areaInfo: areaInfo,
               areaInfoId: areaInfoId,
               provinceName: that.data.provinces[value[0]].name,
               cityName: that.data.citys[value[1]].name,
               districtName: that.data.areas[value[2]].name,
               provinceId: that.data.provinces[value[0]].id,
               cityId: that.data.citys[value[1]].id,
               districtId: that.data.areas[value[2]].id
          })
     },
     hideCitySelected: function (e) {
          console.log(e)
          this.startAddressAnimation(false)
     },
     // 处理省市县联动逻辑
     cityChange: function (e) {
          console.log(e)
          var value = e.detail.value
          var provinces = this.data.provinces
          var citys = this.data.citys
          var areas = this.data.areas
          var provinceNum = value[0]
          var cityNum = value[1]
          var countyNum = value[2]
          if (this.data.value[0] != provinceNum) {
               var id = provinces[provinceNum].id
               this.setData({
                    value: [provinceNum, 0, 0],
                    citys: address.citys[id],
                    areas: address.areas[address.citys[id][0].id],
               })
          } else if (this.data.value[1] != cityNum) {
               var id = citys[cityNum].id
               this.setData({
                    value: [provinceNum, cityNum, 0],
                    areas: address.areas[citys[cityNum].id],
               })
          } else {
               this.setData({
                    value: [provinceNum, cityNum, countyNum]
               })
          }
     },
})